﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, UserRoleDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { RequirementPackageDto,UsableRangeDto,DepartmentServiceProxy,RequirementPackageServiceProxy} from '@shared/service-proxies/service-proxies';
import { AbpSessionService } from '@abp/session/abp-session.service';
@Component({
    selector: 'createOrEditRequirementPackageModal',
    templateUrl: './create-or-edit-requirement-package-modal.component.html'
})
export class CreateOrEditRequirementPackageModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    canChangeUserName = true;
    isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
    isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();

    requirementPackage: RequirementPackageDto = new RequirementPackageDto();
    roles: UserRoleDto[];
    sendActivationEmail = true;
    setRandomPassword = true;
    passwordComplexityInfo = '';
    profilePicture: string;

    allOrganizationUnits: OrganizationUnitDto[];
    memberedOrganizationUnits: string[];
    editFlag = true;
    unitOfUsings: Array<object>;;

    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _requirementPackageService: RequirementPackageServiceProxy,
        private _sessionServiceProxy: AbpSessionService,
        private _profileService: ProfileServiceProxy,
        private _departmentServiceProxy: DepartmentServiceProxy,
    ) {
        super(injector);
        this.getunitOfUsings();
    }
    getunitOfUsings() {
        this.unitOfUsings  = new Array<object>()
        this._departmentServiceProxy.getAllDpts()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.unitOfUsings = result;
            });
    }
    show(requirementPackage?: RequirementPackageDto): void {
        if (requirementPackage) {
            this.requirementPackage = requirementPackage;
            this.editFlag = false;
        } else {
            this.requirementPackage = new RequirementPackageDto();
            this.requirementPackage.usableRange= new UsableRangeDto();
            this.requirementPackage.state = 0;
            this.editFlag = true;
        }
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        document.getElementById('displayName').focus();
    }

    save(): void {
        this.saving = true;
        let userId:string = this._sessionServiceProxy.userId.toString();
        if(!this.requirementPackage.id){
            this.requirementPackage.state=0;
            this._requirementPackageService.createPackage(this.requirementPackage)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });        
        }else{
            this._requirementPackageService.updatePackage(this.requirementPackage)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
        }
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
